package com.hndzyu.yd.home;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.MeterialBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.Util;
import com.hndzyu.yd.widget.CircleImageView;
import com.hndzyu.yd.widget.MyGridView;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class SourceAdapter extends RecyclerView.Adapter<SourceAdapter.ViewHolder> {
    private Context mContext;
    private List<MeterialBean.SucaiBean.DataBean> mData = new ArrayList<>();
    private SourceListener mListener;

    public void setListener(SourceListener listener) {
        mListener = listener;
    }

    public SourceAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<MeterialBean.SucaiBean.DataBean> dataSet,int page) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page==1){
                mData = dataSet;
            }else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_source, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final MeterialBean.SucaiBean.DataBean dataBean = mData.get(position);
        Glide.with(mContext).load("").apply(new RequestOptions().error(R.mipmap.logo))
                .into(holder.civ);
        holder.tvTime.setText(Util.stampToDateDd(dataBean.getAddtime()+""));
        String textStr1 = dataBean.getContent() + "<font color=\"#32A5F8\">【点击复制】</font><br>";
        holder.tvContext.setText(Html.fromHtml(textStr1));
        List<String> url = Arrays.asList(dataBean.getImg().split(","));
        holder.gridView.setAdapter(new ImageAdapter(mContext,url));
        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(mContext,i+"",Toast.LENGTH_SHORT).show();
            }
        });
        holder.tvContext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.copyText(dataBean.getContent());
            }
        });
        //查看二维码
        holder.tvEwm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.shareEwm();
            }
        });
        //保存图文
        holder.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.saveTI(dataBean);
            }
        });
        //一键推广
        holder.tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(cmp);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.civ)
        CircleImageView civ;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_context)
        TextView tvContext;
        @BindView(R.id.gridView)
        MyGridView gridView;
        @BindView(R.id.tv_ewm)
        TextView tvEwm;
        @BindView(R.id.tv_save)
        TextView tvSave;
        @BindView(R.id.tv_share)
        TextView tvShare;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface SourceListener{
        void copyText(String text);
        //二维码
        void shareEwm();
        //保存图文
        void saveTI(MeterialBean.SucaiBean.DataBean item);
        //一键推广
        void extension(MeterialBean.SucaiBean.DataBean item);
    }

    class ImageAdapter extends BaseAdapter {
        private List<String> datas;
        private Context mContext;

        public ImageAdapter(Context mContext, List<String> datas) {
            this.mContext = mContext;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_source_image, null);
                vh.ivImage = (ImageView) view.findViewById(R.id.iv_img);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            Log.d("____________",Path.BASE_URL + datas.get(i));
            Glide.with(mContext).load(Path.BASE_URL + datas.get(i))
                    .into(vh.ivImage);
            return view;
        }

        class ViewHolder {
            public ImageView ivImage;
        }
    }
}
